package com.bohan.rootpage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.Map.Entry;

import com.bohan.main.Main;


public class RootPage {

	Vector <Integer> [] matrixNeigh;
	//float [][] linkMatrix;
	HashMap <String, Integer> linkMat;
	public RootPage(int size){
		//linkMatrix = new float[size][size];
		linkMat = new HashMap<String, Integer>();
	}
	public void setNeigh(int size)
	{
		matrixNeigh = new Vector[size];
		for(int i = 0; i < size ; i ++)
			matrixNeigh[i] = new Vector<Integer>();
		Main.randommax = size;
	}
	public Vector <Integer>[] getNeigh()
	{
		return matrixNeigh;
	}
	public void readFile(String filename) throws NumberFormatException, IOException
	{
		File f = new File(filename);
		InputStream in = new FileInputStream(f);
		InputStreamReader ir = new InputStreamReader(in);
		BufferedReader br = new BufferedReader(ir);
		int ch = -1, first2Count = 0, firstNum = 0, secondNum = 0;
		String num = "";
		String firstStr = "", secondStr = "";
		while( (ch = br.read()) != -1 )
		{
			num += (char)ch;
			if(ch == '\t')
			{
				first2Count ++;
				if(first2Count == 1)
				{
					firstStr = num.trim();
					firstNum = Integer.parseInt(firstStr);
					//firstNum = Integer.parseInt(num.trim());
				}
				if(first2Count == 2)
				{
					secondStr = num.trim();
					secondNum = Integer.parseInt(secondStr);
					if(firstNum < Main.datarange && secondNum < Main.datarange){
						if(linkMat.get(firstStr + "%" + secondStr) == null )
						{
							linkMat.put(firstStr + "%" + secondStr, 1);
							linkMat.put(secondStr + "%" + firstStr, 1);
					//if(linkMatrix[firstNum][secondNum] == 0){
							matrixNeigh[firstNum].add(secondNum);
							matrixNeigh[secondNum].add(firstNum);
						//linkMatrix[firstNum][secondNum] = linkMatrix[secondNum][firstNum] = 1;
						}
					}
				}
				num = "";
			}
			if(ch == '\n'){
				first2Count = 0;
				num = "";
			}
		}
		br.close();
		ir.close();
		in.close();
	}
	public void clear(Vector <Integer> [] neigh)
	{
		linkMat = null;
		neigh = null;
	}
	public void compute(Vector <Integer> [] neigh, int size)
	{
		Random random = new Random();
		//System.out.println(Main.randommax);
		Map <Integer, Integer> m = new HashMap<Integer, Integer>();
		for (int i = 0; i < size; i++) { 
	        int vertex = i;
	        
	        int currentVertex = vertex; 
	       
	        for(int step = 1; true; step++ ) { 
	            int rand = random.nextInt(Main.randommax + 1);
	        	if( neigh[currentVertex].size() == 0 || (double)rand / Main.randommax < Main.alpha ) { 
	            //if( neigh[currentVertex].size() == 0 ) {
	                currentVertex = vertex; 
	            } else {                     
	                int randomNeighbor = rand % neigh[currentVertex].size(); 
	                currentVertex = neigh[currentVertex].get(randomNeighbor); 
	            } 
	       
	            if( m.get(currentVertex) == null)
	            	m.put(currentVertex, 1);
	            else
	            	m.put(currentVertex, m.get(currentVertex) + 1);       
	            if( step == Main.step ) { 
	            //    copy();
	            	break;
	            } else if( step % Main.step == 0 ) {
	            	/*
	                double r = sampleCorrelationCoefficient( oldRank, rank ); 
	                if( r > 0.99 )
	                    break;
	                 else
	                    copy(); 
	                  */
	            }
	        }
	        
	        List< Map.Entry<Integer,Integer> > list = new ArrayList< Map.Entry<Integer,Integer> >(m.entrySet());
	        Collections.sort(list,new Comparator< Map.Entry<Integer,Integer> >() {
	            
				public int compare(Entry<Integer, Integer> o1,
						Entry<Integer, Integer> o2) {
					return o2.getValue().compareTo(o1.getValue());
				}

	        });
	        int top = 0 ;
	        for(Map.Entry<Integer, Integer> mapping : list){
	        		top ++;
	        		if(top > Main.topN)
	        			break;
	        		//System.out.println(mapping.getKey()+":"+mapping.getValue());
	        }
	        if(i % 10000 == 0)
	        	System.out.println(i);
		}
	}
}
